package com.javatiaocao.myblog.service.impl;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.mapper.RewardMapper;
import com.javatiaocao.myblog.model.Reward;
import com.javatiaocao.myblog.service.RewardService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.FileUtil;
import com.javatiaocao.myblog.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class RewardServiceImpl implements RewardService {

    @Autowired
    private RewardMapper rewardMapper;

    /**
     * 获取募捐信息
     * @return
     */
    @Override
    public DataMap getRewardInfo() {

        ArrayList<HashMap> result = new ArrayList<>();
        //fundRaiser,id,rewardMoney,fundRaisingSources,fundraisingPlace
        //remarks,rewardDate
        List<Reward> rewardList = rewardMapper.getRewardInfo();
        if (rewardList == null) {
            return DataMap.fail(CodeType.SERVER_EXCEPTION);
        }
        for (Reward reward : rewardList) {
            HashMap<String, Object> object = new HashMap<>();
            object.put("id", reward.getId());
            object.put("fundRaiser", reward.getFundRaiser());
            object.put("rewardMoney", reward.getRewardMoney());
            object.put("fundRaisingSources", reward.getFundRaisingSources());
            object.put("fundraisingPlace", reward.getFundraisingPlace());
            object.put("remarks", reward.getRemarks());
            object.put("rewardDate", reward.getRewardDate());
            result.add(object);
        }


        return DataMap.success(CodeType.SUCCESS_STATUS).setData(result);
    }

    @Override
    public DataMap addReward(MultipartFile file, HttpServletRequest request, Reward reward) {
        //获取时间
        String rewardDate = request.getParameter("reward-date");
        //上传附件
        FileUtil fileUtil = new FileUtil();
        //获取文件路径
        String filePath = this.getClass().getResource("/").getPath().substring(1) + "blogImg/";
        String contentType = file.getContentType();
        String fileSub = contentType.substring(contentType.indexOf("/") + 1);
        TimeUtil timeUtil = new TimeUtil();
        String fileName = timeUtil.getLongTime() + "." + fileSub;//服务器名称
        String fileCatalog = "rewardRecord" + timeUtil.getFormatDateForThree();
        //String fileUrl = fileUtil.uploadFile(fileUtil.multipartFileToFile(file, filePath, fileName), fileCatalog);//上传到服务器

        reward.setRewardDate(timeUtil.stringToDateThree(rewardDate));
        reward.setRewardUrl("fileUrl");
        rewardMapper.addReward(reward);
        return DataMap.success(CodeType.SUCCESS_STATUS);
    }

    @Override
    public DataMap deleteReward(Reward reward) {

        int result = rewardMapper.deleteReward(reward);

        return DataMap.success(CodeType.SUCCESS_STATUS).setData(result);
    }


}
